package com.wafer.server;

import java.io.IOException;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.wafer.server.http.HttpRequest;
import com.wafer.server.util.StreamUtil;


/**
 *  线程实现类，当有连接请求到来时处理连接请求
 *
 */

public final class ServerThread implements Runnable{
	
	private static Logger logger = Logger.getLogger(ServerThread.class);
	
	private Socket socket;

	public ServerThread(Socket socket){
		this.socket = socket;
	}

	/**
	 * 从socket中获取请求数据，并将请求数据封装成HttpServletRrquest格式
	 */
	public void run() {	
		logger.debug("线程" + Thread.currentThread().getName() + "is handling request");		
		try {
			   HttpRequest request = StreamUtil.streamToRequest(socket);  // 将socket获取的TCP消息转换成 HttpServletRequest格式
			   StreamUtil.putResponseToSocket(null, socket);
			   socket.close();
			   logger.debug("请求处理结束");
			   
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

}





























